import userStore from "@/store/userStore"
import {
  clearStorage
} from "@/utils/storage"
import {
  ComponentWithStore
} from "mobx-miniprogram-bindings"
import {
  reqLogout
} from '@/api/index'

ComponentWithStore({
  data: {
    defaultAvatar: "https://yi-ban-jia.oss-cn-beijing.aliyuncs.com/default.png"
  },
  storeBindings: {
    store: userStore,
    fields: ['userInfo', 'token'],
    actions: ['setUserInfo', 'setToken']
  },
  methods: {
    toLogin() {
      wx.redirectTo({
        url: '/pages/login/login',
      })
    },
    async logout() {
      await reqLogout()
      this.setUserInfo({})
      this.setToken('')
      clearStorage()
    },
    toUserInfo() {
      if (this.data.token) {
        wx.navigateTo({
          url: '/modules/myModule/pages/profile/profile',
        })
      } else {
        wx.navigateTo({
          url: '/pages/to-login/to-login?message=登录后修改用户信息',
        })
      }
    },
    toCommentList() {
      if (this.data.token) {
        wx.navigateTo({
          url: '/pages/comment/comment',
        })
      } else {
        wx.navigateTo({
          url: '/pages/to-login/to-login?message=登录后查看交易评价',
        })
      }
    },
    call(){
      wx.makePhoneCall({
        phoneNumber: '13608411380',
        success:function(){
          console.log('拨打电话成功')
        },
        fail:function(){
          console.log('拨打电话失败')
        }
      })
    }
  }
})